package dk.liljedahl.christian.apx.shipdaemon;

import org.jivesoftware.smack.packet.Message;


/**
 * @author ChristianLiljedahl
 *
 */
public class CheckArduinoParameters {
	static Mediator myMediator;
	
	
	/**
	 * Used to give the event scripts ability to keep track of stuff from one run to the next
	 */
	//public int[] scriptmemory_int = new int[30];

	public CheckArduinoParameters(Mediator mediator) {
		CheckArduinoParameters.myMediator = mediator;
		
		// Reset memory, to avoid null-pointer-fuck
		for(int i = 0;i<30;i++){
			myMediator.scriptmemory_int[i] = 0;
		}

	}
	
	public void setPinModes(String script){
		// TODO - remove
	}

	public void checkParameters(String script){
		// Get all the parameters from arduino

		ExpressionParser expParser = new ExpressionParser();

		// Get the analog
		for(int i = 0;i<6;i++){
			expParser.analog[i] = myMediator.arduinoFirmata.analogRead(i);
		}

		// Get the digital
		for(int i = 0;i<14;i++){
			// TODO - Intelligence would state to only do checks depending on pinmode
			expParser.digital[i] = myMediator.arduinoFirmata.digitalRead(i);
			// Set the digitalwrite to -1 to know if it has been changed
			expParser.digitalwrite[i] = -1;
			// Set the analogwrite to -1 to know if it has been changed
			expParser.analogwrite[i] = -1;
		}


		// Set message to "" to avoid the fucking null-pointer check
		for(int i = 0;i<30;i++){
			expParser.message[i] = "";
			expParser.messagerecipients[i] = "";
		}
		
		// inject the scriptmemory data to let the script change the memory.
		// TODO - THis might be a stupid way of coding
		for(int i = 0;i<30;i++){
			expParser.scriptmemory_int[i] = myMediator.scriptmemory_int[i];
		}

		// set the status for presence
    	// TODO - yet another silly [0 - this for presence status
    	expParser.status[0] = "";


		// Load them into the ExpressionParser
		expParser.script = script;
		// Run the expression parser
		expParser.evaluateEventScript();

		// extract the scriptmemory data to save them.
		for(int i = 0;i<30;i++){
			myMediator.scriptmemory_int[i] = expParser.scriptmemory_int[i];
		}
		
		// Extract the digitalwrite and analogwrite
		for(int i = 0;i<14;i++){
			// Digital write
			int writevalueDigital = expParser.digitalwrite[i];
			if(writevalueDigital > -1 && writevalueDigital <=1){
				// -1 is magic number - Something was changed
				myMediator.arduinoFirmata.digitalWrite(i, writevalueDigital); 
			}
			
			// Analog write. We hope that firmata keeps track of pwn and don't fuck it up
			int writevalueAnalog = expParser.analogwrite[i];
			if(writevalueAnalog > -1 && writevalueAnalog <=255){
				// -1 is magic number - Something was changed
				myMediator.arduinoFirmata.analogWrite(i, writevalueAnalog); 
			}
		}

		// Extract status - if != "" then change the presence status 
		if(!expParser.status[0].equals("")){
			// We have a status message
			myMediator.xmppmachine.setStatus(expParser.status[0]);
		}

		
		// Loop through message and messagerecipients 
		// Determine if a message should be sent and send it.
		// Do we have any messages at all? 
		for (int i = 0;i<30;i++)
		{
			// If nothing is in the message, nothing should be sent
			if(!expParser.message[i].equals("")){				
				if(expParser.messagerecipients[i].equals("ALL")){
					
					myMediator.xmppmachine.SendToAllFriends(expParser.message[i]);					

				} else {
					// Loop through the list of recipients and send each a message
					String recipientlist = expParser.messagerecipients[i];
					String[] recipients = recipientlist.split(",");
					for(String r : recipients){
						myMediator.xmppmachine.SendToFriend(expParser.message[i],r);
					}
				}
			}
		}
	}
}
